#!/usr/bin/env python

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'manager_login_form.ui'
#
# Created: Thu Apr 30 02:35:41 2009
#      by: The PyQt User Interface Compiler (pyuic) 3.17.6
#
# WARNING! All changes made in this file will be lost!


from qt import *
from sys import exit
from manage_form import *


class manager_login_form(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if not name:
            self.setName("manager_login_form")


        self.Login_box = QGroupBox(self,"Login_box")
        self.Login_box.setGeometry(QRect(20,20,590,80))

        self.textLabel2 = QLabel(self.Login_box,"textLabel2")
        self.textLabel2.setGeometry(QRect(20,30,170,20))

        self.manager_password_text = QLineEdit(self.Login_box,"manager_password_text")
        self.manager_password_text.setEchoMode(QLineEdit.Password)
        self.manager_password_text.setGeometry(QRect(200,30,371,21))

        self.manager_login_button = QPushButton(self,"manager_login_button")
        self.manager_login_button.setGeometry(QRect(500,110,110,24))

        self.cancel_login_manager_btn = QPushButton(self,"cancel_login_manager_btn")
        self.cancel_login_manager_btn.setGeometry(QRect(380,110,110,24))

        self.languageChange()

        self.resize(QSize(618,146).expandedTo(self.minimumSizeHint()))
        self.clearWState(Qt.WState_Polished)

        self.connect(self.manager_login_button,SIGNAL("clicked()"),self.manager_login_button_clicked)
        self.connect(self.cancel_login_manager_btn,SIGNAL("clicked()"),self.cancel_login_manager_btn_clicked)


    def languageChange(self):
        self.setCaption(self.__tr("Manage adminstrators DB"))
        self.Login_box.setTitle(self.__tr("Manager Login"))
        self.textLabel2.setText(self.__tr("Manager password"))
        self.manager_login_button.setText(self.__tr("&Login"))
        self.manager_login_button.setAccel(QKeySequence(self.__tr("Alt+L")))
        self.cancel_login_manager_btn.setText(self.__tr("&Cancel"))
        self.cancel_login_manager_btn.setAccel(QKeySequence(self.__tr("Alt+C")))


    def cancel_login_manager_btn_clicked(self):
        '''
        cancel_login_manager_btn_clicked(self):
            if clicked the window is hidden
            manager login
        '''
        self.hide()

    def cancel_login_manager_btn_destroyed(self,a0):
        pass

    def manager_login_button_clicked(self):
        '''
        manager_login_button_clicked(self):
            this function check if the manager password matched or not!
        '''

        #open the configuration file
        conf_file = open('discovery.conf')

        #declare the options dictionary
        options_dictionary = {}

        #initialize the options dictionary
        for raw_option in conf_file :

            option = raw_option.strip().split()

            #take only two arguments lines
            if len(option) == 2 :
                options_dictionary[option[0]] = option[1]

        #close the configuration file
        conf_file.close()


        password_entered = self.manager_password_text.text()

        try :
            if password_entered == options_dictionary['MANAGER_PASSWORD']:
                manager_window = manage_form()
                manager_window.show()
                self.hide()
                manager_window.exec_loop()
            else:
                Message = 'You entered wrong password!'
                QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)
        except :
            Message = 'You entered wrong password!'
            QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)

    def __tr(self,s,c = None):
        return qApp.translate("manager_login_form",s,c)
